﻿using System;
using System.Collections.Generic;
using System.DrawingCore;
using System.DrawingCore.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SY_Library
{
    public static class ImageDispose
    {
        /// <summary>
        /// 转换为正方形
        /// </summary>
        /// <param name="image">目标图片</param>
        public static Image ToSquare(this Image image)
        {
            var width = Convert.ToInt32(image.Width);
            var height = Convert.ToInt32(image.Height);
            var sumNum = Math.Min(width, height);

            Rectangle cropRegion;
            if (sumNum == width)
                cropRegion = new Rectangle(0, (height - sumNum) / 2, sumNum, sumNum);
            else
                cropRegion = new Rectangle((width - sumNum) / 2, 0, sumNum, sumNum);

            Bitmap newImage = new Bitmap(cropRegion.Width, cropRegion.Height);
            Graphics graphics = Graphics.FromImage(newImage);
            graphics.DrawImage(image, new Rectangle(0, 0, cropRegion.Width, cropRegion.Height), cropRegion, GraphicsUnit.Pixel);

            return newImage;
        }

        /// <summary>
        /// 缩放
        /// </summary>
        /// <param name="image">目标图片</param>
        /// <param name="magnification">缩放倍数</param>
        /// <returns></returns>
        public static Image ToZoom(this Image image, int magnification)
        {
            if (magnification > 0)
            {
                return image.GetThumbnailImage(image.Width / magnification, image.Height / magnification, () => { return false; }, IntPtr.Zero);
            }
            else if (magnification < 0)
            {
                return image.GetThumbnailImage(image.Width / (-1) * magnification, image.Height / (-1) * magnification, () => { return false; }, IntPtr.Zero);
            }
            else
            {
                return image;
            }
        }

        /// <summary>
        /// 转换为Icon并保存
        /// </summary>
        /// <param name="image">目标图片</param>
        /// <param name="path">保存路径</param>
        /// <param name="size">指定大小</param>
        public static bool ToIconSave(this Image image, string path, Size size)
        {
            if (size.Width > 255 || size.Height > 255)
            {
                return false;
            }
            MemoryStream bitMapStream = new MemoryStream(); //存原图的内存流
            MemoryStream iconStream = new MemoryStream(); //存图标的内存流
            image.Save(bitMapStream, ImageFormat.Png); //将原图读取为png格式并存入原图内存流
            BinaryWriter iconWriter = new BinaryWriter(iconStream); //新建二进制写入器以写入目标图标内存流

            iconWriter.Write((short)0);
            iconWriter.Write((short)1);
            iconWriter.Write((short)1);
            iconWriter.Write((byte)image.Width);
            iconWriter.Write((byte)image.Height);
            iconWriter.Write((short)0);
            iconWriter.Write((short)0);
            iconWriter.Write((short)32);
            iconWriter.Write((int)bitMapStream.Length);
            iconWriter.Write(22);
            //写入图像体至目标图标内存流
            iconWriter.Write(bitMapStream.ToArray());
            //保存流，并将流指针定位至头部以Icon对象进行读取输出为文件
            iconWriter.Flush();
            iconWriter.Seek(0, SeekOrigin.Begin);
            Stream iconFileStream = new FileStream(path, FileMode.Create);
            Icon icon = new Icon(iconStream);
            icon.Save(iconFileStream); //储存图像

            iconFileStream.Close();
            iconWriter.Close();
            iconStream.Close();
            bitMapStream.Close();
            icon.Dispose();
            image.Dispose();
            return File.Exists(path);
        }

        /// <summary>
        /// 添加水印
        /// </summary>
        /// <param name="map">水印图片</param>
        /// <param name="rectangle">指定方位</param>
        /// <param name="opacity">透明度</param>
        /// <returns></returns>
        public static Image Draw(this Image image, Image toImage, Rectangle rectangle, double? opacity = null)
        {
            Graphics graphics = Graphics.FromImage(image);
            var map = toImage as Bitmap;
            if (opacity != null)
            {
                for (int i = 0; i < map.Width; i++)
                {
                    for (int j = 0; j < map.Height; j++)
                    {
                        System.DrawingCore.Color color = map.GetPixel(i, j);
                        map.SetPixel(i, j, System.DrawingCore.Color.FromArgb((int)(color.A * opacity), color.R, color.G, color.B));
                    }
                }
            }

            graphics.DrawImage(map, rectangle.X, rectangle.Y);
            return image;
        }
    }
}
